import 'dart:io';

import 'package:dict_page_view_plugin/dict_page_view_widget_controller.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

typedef TOnDictPageViewCreated = void Function(DictPageViewWidgetController controller);

class DictPageViewWidget extends StatefulWidget {
  DictPageViewWidget({super.key, required this.onDictPageViewCreated});

  TOnDictPageViewCreated onDictPageViewCreated;

  @override
  State<StatefulWidget> createState() {
    return _DictPageViewWidgetState();
  }
}

class _DictPageViewWidgetState extends State<DictPageViewWidget> {
  _DictPageViewWidgetState();
  DictPageViewWidgetController? controller;

  @override
  void initState() {
    super.initState();


  }
  @override
  Widget build(BuildContext context) {
    if (Platform.isAndroid) {
      return AndroidView(
          viewType: "com.maiyuren.sudict/dictpageview",
          onPlatformViewCreated: (viewId) {
            controller = DictPageViewWidgetController(id: viewId);
            widget.onDictPageViewCreated(controller!);
          },
        );
    } else if (Platform.isIOS) {
      return UiKitView(
        viewType: "com.maiyuren.sudict/dictpageview",
        onPlatformViewCreated: (viewId) {
          controller = DictPageViewWidgetController(id: viewId);
          widget.onDictPageViewCreated(controller!);
        },
      );
    }

    return const Text("Not Implemented");
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }
}
